$(document).ready(function() {
    if (location.hash == '#login') {
        $(".login-box.js-register").hide();
        $(".login-box.js-login").show();
    }else if(location.hash == '#register'){
        $(".login-box.js-register").show();
        $(".login-box.js-login").hide();
    }
    $(".login-box .js-register-first").show();
    $(".login-box .js-register-next").hide();
    $(".js-log").click(function () {
        toggleA()
    });
    $(".js-reg").click(function () {
        toggleA()
    });
    $(".login-box .js-switch-button").click(function () {
        toggleA()
    });
    $(".login-box .js-next-button").click(function () {
        toggleB()
    });
    $(".login-box .js-back-button").click(function () {
        toggleB()
    });
    //登陆注册切换
    console.log(location.hash);
    function toggleA() {
        if (location.hash == '#register') {
            location.hash='#login';
        }else{
            location.hash='#register';
        }
        $(".login-box.js-register").toggle(0);
        $(".login-box.js-login").toggle(0);
    }
    
    注册两步切换
    function toggleB() {
        if (location.hash == '#register-next') {
            location.hash='#register-first';
        }else{
            location.hash='#register-next';
        }
        $(".login-box .js-register-next").toggle(0);
        $(".login-box .js-register-first").toggle(0);
    }

    //注册表单验证
    //login-form
    var userInput = $('#user')
    var passInput = $('#pass')

    //register-form
    var usernameInput = $('#username')
    var passwordInput = $('#password')
    var resetInput = $('#reset')
    var truenameInput = $('#truename')
    var emailInput = $('#email')
    var phoneInput = $('#phone')
    var companyInput = $('#company')
    var schoolInput = $('#school')
    var graduationInput = $('#graduation')


    var logForm  = $('#login-form');
        logForm.submit(function(event) {
            var user  = userInput.val();
            var code = passInput.val();
            if(!user||!code){
                alert('请填写完整');
                event.preventDefault();
            }
         
            return true;
        });

    var regForm  = $('#register-form');
        //密码验证
        passwordInput.change(function(event) {
            var pasw = passwordInput.val();
            var res = resetInput.val();
            if(!pasw){alert("请设置密码");}
            else if(pasw.length<6){
                alert('密码不少于六位');
                event.preventDefault();
            }
        });
        //再次输入密码
        resetInput.change(function(event) {
            var pasw = passwordInput.val();
            var res = resetInput.val();
            if(!pasw){alert("请再次输入密码");}
            else if(pasw!=res){
                alert('两次输的密码不一样');
                event.preventDefault();
            }
        });
        
        regForm.submit(function(event) {
            var usn  = usernameInput.val();
            var pasw = passwordInput.val();
            var res = resetInput.val();
            var trun = truenameInput.val();
            var email = emailInput.val();
            var phone = phoneInput.val();
            var company = companyInput.val();
            var school = schoolInput.val();
            var graduation = graduationInput.val();

            if(!usn||!pasw||!res||!trun ||!email||!phone||!company||!school||!graduation){
                alert('请填写完整');
                $(".login-box .js-register-next").toggle(0);
                $(".login-box .js-register-first").toggle(0);
                event.preventDefault();
            }
            checkmail();
            checkTel();
            if(!usn||!pasw||!res||!trun ||!checkmail()||!checkTel()||!company||!school||!graduation){
                event.preventDefault();
            }
        });

        //邮箱验证
        function checkmail(){
            var mai=emailInput.val();
            var maiExpression=/\w+(-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/;
            var oExp1=new RegExp(maiExpression);
            if(mai&&oExp1.test(mai)==false){
                alert("邮箱格式不正确");
                return false;
            }
            else{return true;}
        }
        emailInput.change(function(event) {
            checkmail();
        });

        //手机号验证
        function checkTel(){
            var tel=phoneInput.val();
            var telExpression=/^(((13[0-9]{1})|(15[0-9]{1}))+\d{8})$/; ///^(?:13\d|15\d|18\d)\d{5}(\d{3}|\*{3})$/
            var oExp2=new RegExp(telExpression);
            if(tel&&oExp2.test(tel)==false){
                alert("手机号格式不正确");
                return false;
            }
            else{return true;}
        }
        phoneInput.change(function(event) {
            checkTel();
        });

});